package cn.cena.service.impl;

import cn.cena.constant.BaseConstant;
import cn.cena.constant.ErrorCode;
import cn.cena.domain.Employee;
import cn.cena.domain.Login;
import cn.cena.domain.Tenant;
import cn.cena.dto.RelationLogin2MealDto;
import cn.cena.dto.TenantRegisterDto;
import cn.cena.exception.GlobalException;
import cn.cena.feign.client.LoginFeignClient;
import cn.cena.mapper.TenantMapper;
import cn.cena.service.IEmployeeService;
import cn.cena.service.ITenantService;
import cn.cena.util.AjaxResult;
import cn.cena.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yu
 * @since 2021-11-20
 */
@Service
@Transactional(rollbackFor = Error.class)
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private LoginFeignClient loginFeignClient;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ITenantService tenantService;

    /**
     *  机构入驻
     * @param dto
     */
    @Override
    public void entering(TenantRegisterDto dto) {
        // 参数判断
        Employee employee = dto.getEmployee();
        Login login = dto.getLogin();
        Long mealId = dto.getMealId();
        Tenant tenant = dto.getTenant();

        // 参数校验 方式一：AssertUtil
        // 方式二 JSR303注解： 对象@Valid 字段：@NotNull(message = "请选择套餐")
        AssertUtil.isNull(mealId, ErrorCode.GEGISTER_MEAL_IS_NULL.getFullMassage());
        AssertUtil.isEmpty(login.getUsername(),ErrorCode.LOGIN_USERNAME_IS_NULL.getFullMassage() );

        // 保存t_login表和t_login_meal
        // 密码加盐 TODO
        // 重复操作 TODO
        RelationLogin2MealDto login2MealDto = new RelationLogin2MealDto();
        login2MealDto.setLogin(login);
        login2MealDto.setMealId(mealId);
        AjaxResult ajaxResult = loginFeignClient.saveLoginAndMeal(login2MealDto);
        AssertUtil.isFalse(ajaxResult.isSuccess() || ajaxResult.getResultObj() != null,"登录用户保存失败" );

        // 取得返回的loginId
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        // 保存t_employee
        employee.setLoginId(loginId);
        employee.setRealName(dto.getTenant().getCompanyName());
        Date date = new Date();
        employee.setInputTime(date);
        employee.setState(BaseConstant.Emp.STATE_NORMAL);
        employee.setType(BaseConstant.Emp.TYPE_TENANT_ADMIN);
        employeeService.insert(employee);

        // 保存t_tenant
        tenant.setAdminId(employee.getId());
        tenant.setRegisterTime(date);
        tenantService.insert(tenant);
        // 获取返回的tenantId
        Long tenantId = tenant.getId();
        employee.setTenantId(tenantId);
        employeeService.updateById(employee);
    }
}
